import Vue from 'vue';
import Router from 'vue-router';

import store from '../store';
import HomePage from '../components/home-page';
import HomeBox from '../components/home-box';
import Login from '../components/login';
import AudioCall from '../components/audio-call';
import VideoCall from '../components/video-call';
import SearchUser from '../components/search-user';
import callPage from '../components/call-page';
import selectType from '../components/select-type';
import healthRecord from '../components/health-record';
import Consultation from '../components/consultation';
import sleepReport from '../components/sleep-report';
import callConfig from '../components/call-config';
import nightCare from '../components/night-care';
import speechService from '../components/speech-service';
import onlineChat from '../components/online-chat';
import updateRecords from '../components/update-records';
import AIchat from '../components/AI-chat';
Vue.use(Router);

export function createRouter () {
  const router = new Router({
    mode: 'hash',
    fallback: false,
    routes: [
      { path: '/', component: HomeBox},
      { path: '/search-user', component: SearchUser},
      { path: '/home-page', component: HomePage},
      { path: '/login', component: Login},
      { path: '/audio-call', component: AudioCall},
      { path: '/video-call', component: VideoCall},
      { path: '/call-page', component: callPage},
      { path: '/select-type', component: selectType},
      { path: '/health-record', component: healthRecord},
      { path: '/consultation', component: Consultation},
      { path: '/sleep-report', component: sleepReport},
      { path: '/call-config', component: callConfig},
      { path: '/night-care', component: nightCare},
      { path: '/speech-service', component: speechService},
      { path: '/online-chat', component: onlineChat},
      { path: '/update-records', component: updateRecords},
      { path: '/AI-chat', component: AIchat}
    ]
  });
  router.beforeEach((to, from, next) => {
    if (!store.state.isLogin) {
      if (to.fullPath !== '/login') {
        if (from.fullPath !== '/login') {
          next('/login');
        }
        return;
      }
    }
    next();
  })
  return router;
}

const originalPush = Router.prototype.push
   Router.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}